package cn.ljl.pet.service.impl;

import cn.ljl.pet.domain.PetType;
import cn.ljl.pet.mapper.PetTypeMapper;
import cn.ljl.pet.service.IPetTypeService;
import cn.ljl.basic.service.impl.BaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * 业务实现类：
 */
@Service
public class PetTypeServiceImpl extends BaseServiceImpl<PetType> implements IPetTypeService {
@Autowired
private PetTypeMapper petTypeMapper;
    @Override
    public List<PetType> petTypeTree() {
        List<PetType> petTypes = petTypeMapper.loadAll();
        HashMap<Long, PetType> map = new HashMap<>();
        for (PetType petType : petTypes) {
            map.put(petType.getId(),petType);
        }
        ArrayList<PetType> petTypeTree = new ArrayList<>();
        for (PetType petType : petTypes) {
            if (petType.getParentId()==null){
                petTypeTree.add(petType);
            }else {
                Long parentId = petType.getParentId();
                PetType type = map.get(parentId);
                if (type!=null){
                    type.getChildren().add(petType);
                }
            }
        }

        return petTypeTree;
    }
}
